/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package net.wofly.sale.activity.repository;

import net.wofly.sale.activity.domain.Activity;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ActivityRepository
        extends PagingAndSortingRepository<Activity, String>, JpaSpecificationExecutor<Activity> {
    
    List<Activity> findByCommodityChannelIDAndStatusAndStartTimeBeforeAndEndTimeAfter(
            String commodityChannelID, Integer status, long nowTime1, long nowTime2);
    
    List<Activity> findByOrderTypeAndCommodityChannelIDAndStatusAndStartTimeBeforeAndEndTimeAfter(
            Integer orderType, String commodityChannelID, Integer status, long nowTime1, long nowTime2);
    
    List<Activity> findByOrderTypeAndActivityTypeAndStatusAndStartTimeBeforeAndEndTimeAfter(
            Integer orderType, Integer activityType, Integer status, long nowTime1, long nowTime2);
    
    List<Activity> findByActivityType(Integer activityType);
    
    List<Activity> findByActivityTypeAndStatusAndStartTimeBeforeAndEndTimeAfter(
            Integer activityType, Integer status, long nowTime1, long nowTime2);
    
    List<Activity> findByActivityIDIn(List<String> activityIDs);
    
    @Query(value = "select a from Activity a where orderType = :orderType and commodityChannelID = :commodityChannelID and status =1 and ((startTime >= :startTime and startTime < :endTime) or (endTime > :startTime and endTime < :endTime))")
    List<Activity> findByCommodityChannelIDAndStartTimeAndEndTime(@Param("orderType")Integer orderType, @Param("commodityChannelID")String commodityChannelID, @Param("startTime")long startTime, @Param("endTime")long endTime);
    
    @Query(value = "select a from Activity a where orderType = :orderType and status =1 and ((startTime >= :startTime and startTime >= :endTime) or (endTime >= :startTime and endTime >= :endTime))")
    List<Activity> findByOrderTypeAndStartTimeAndEndTime(@Param("orderType")Integer orderType, @Param("startTime")long startTime, @Param("endTime")long endTime);
    
    
    List<Activity>  getActivityByOrderTypeAndActivityType(Integer orderType, Integer activityType) ;
}